package com.monkeygroup.mygoplus.view.home.activity.sell;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jude.rollviewpager.OnItemClickListener;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.LoopPagerAdapter;
import com.jude.rollviewpager.hintview.ColorPointHintView;
import com.monkeygroup.mygoplus.R;
import com.monkeygroup.mygoplus.base.BaseActivity;
import com.monkeygroup.mygoplus.commit.thing.vo.PropertyCommitVO;
import com.monkeygroup.mygoplus.common.DefinedView.CustomTextWatcher;
import com.monkeygroup.mygoplus.common.DefinedView.MyAlertDialog;
import com.monkeygroup.mygoplus.common.DefinedView.PickerView;
import com.monkeygroup.mygoplus.common.imageview.ImageLoader;
import com.monkeygroup.mygoplus.common.utils.LocalInfo;
import com.monkeygroup.mygoplus.common.utils.StringUtil;
import com.monkeygroup.mygoplus.common.utils.Tools;
import com.monkeygroup.mygoplus.http.RequestUrl;
import com.monkeygroup.mygoplus.http.ThingsHttpModel;
import com.monkeygroup.mygoplus.thing.vo.ChooseImgVO;
import com.monkeygroup.mygoplus.thing.vo.RentVO;
import com.monkeygroup.mygoplus.thing.vo.ThingImageVO;
import com.monkeygroup.mygoplus.thing.vo.ZoneVO;
import com.monkeygroup.mygoplus.view.home.activity.PublishedChooseAddrActivity;
import com.monkeygroup.mygoplus.view.home.activity.PublishedChooseImageActivity;
import com.monkeygroup.mygoplus.view.home.activity.PublishedChooseShopEquipActivity;
import com.monkeygroup.mygoplus.view.home.activity.PublishedImageActivity;
import com.monkeygroup.mygoplus.view.home.activity.ThingsDetailActivity;



import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 新增土地
 * Created by cumt on 2017/5/27.
 */

public class PublishedLandActivity extends BaseActivity implements View.OnClickListener{

    public static void gotoActivity(Activity activity, String type) {
        Intent intent = new Intent(activity,
                PublishedLandActivity.class);

        intent.putExtra("type", type);

        activity.startActivity(intent);
    }



    private Activity activity;
    private TextView txtHouse,txtModal;
    private EditText etName,etCommunity,etSellprice;

    private EditText etAddr,et_descrip,etSquare;
    private EditText et_landcg,et_landapplication,et_landattribute,et_locate1,et_locate2;
    private LinearLayout layout_shortcut,llRoomAge,llFitting,llBottomSee,llPropertyBottom;

    private TextView spinner_room_age,spinner_fitting,spinner_layouttype,choose_month_unit,choose_year_unit;

    private CheckBox check_lift,check_carport;
    private LinearLayout ll_img_item,ll_img,ll_community;

    private ThingsHttpModel propertyModel,communityHttpModel;
    private PropertyCommitVO requestVO;
    private Map<String,String> params;

    private ImageView focusDown,focusUp,myfocusDown,myfocusUp;

    private String type;

    private PopupWindow pop,communitypop,pickerpop;
    private PickerView picker,choose_month_picker;

    private ImageView imgBack;
    private TextView txtTitle;

    private RelativeLayout relImage;

    private String landapplication,landattribute;
    private List<RentVO> communityList;
    private CommunityAdapter communityAdapter;

    private Map<String,ChooseImgVO> chooseImg;
    //选择的图片类别
    private Map<String,ThingImageVO> mytype=null;

    @Override
    public void onClick(View v) {

    }

    @Override
    public void setContentView() {
        setContentView(R.layout.published_land_content);
    }

    @Override
    public void initView() {

        activity = PublishedLandActivity.this;
//           requestVO = new PropertyCommitVO();
//        Log.e("alInfo.getHou::;",""+LocalInfo.getLandProperty(PublishedLandActivity.this));

        imgBack = (ImageView) findViewById(R.id.img_back);
        txtTitle = (TextView) findViewById(R.id.txt_title);
        layout_shortcut= (LinearLayout) findViewById(R.id.layout_shortcut);
        layout_shortcut.setVisibility(View.INVISIBLE);

        txtHouse=(TextView)findViewById(R.id.txt_house);
        txtModal = (TextView) findViewById(R.id.txt_modal);
        etName =(EditText) findViewById(R.id.et_name);
      //  et_landcg = (EditText) findViewById(R.id.et_landcg);
        et_landapplication= (EditText) findViewById(R.id.et_landapplication);
        et_landattribute= (EditText) findViewById(R.id.et_landattribute);
        etSquare=(EditText) findViewById(R.id.et_square);

        et_locate1=(EditText) findViewById(R.id.et_locate1);
        et_locate2=(EditText) findViewById(R.id.et_locate2);

        etCommunity = (EditText)findViewById(R.id.et_community);
        etSellprice= (EditText) findViewById(R.id.et_sellprice);
        etAddr= (EditText) findViewById(R.id.et_addr);

        relImage = (RelativeLayout) findViewById(R.id.rel_image);

        ll_community= (LinearLayout)findViewById(R.id.ll_community);


//        spinner_month_price=(TextView)findViewById(R.id.spinner_month_price);
//        spinner_room_age=(TextView)findViewById(R.id.spinner_room_age);

//        spinner_layouttype=(TextView)findViewById(R.id.spinner_layouttype);

        communityList = new ArrayList<>();
        params = new HashMap<>();

        check_lift=(CheckBox)findViewById(R.id.check_lift);
        check_carport=(CheckBox)findViewById(R.id.check_carport);


//        llRoomAge= (LinearLayout)  findViewById(R.id.ll_room_age);
//        llFitting = (LinearLayout)findViewById(R.id.ll_fitting);
        llPropertyBottom= (LinearLayout)findViewById(R.id.ll_property_bottom);


        et_descrip = (EditText)findViewById(R.id.et_descrip);
        llBottomSee= (LinearLayout) findViewById(R.id.ll_bottom_see);

        myfocusDown= (ImageView) findViewById(R.id.focus_down);
        myfocusUp= (ImageView) findViewById(R.id.focus_up);

        picker = (PickerView)findViewById(R.id.choose_picker);

        ll_img_item= (LinearLayout)findViewById(R.id.ll_img_item);
        ll_img= (LinearLayout)findViewById(R.id.ll_img);

        chooseImg=new HashMap<>();
        mytype= new HashMap<String,ThingImageVO>();

        Intent intent = this.getIntent();
        Bundle bundle =  intent.getExtras();
        type = bundle.getString("type");

        initData();
        fillData();
        initCheck();
        initRequest();
        initWatcher();
        initPopwindow();

        initCommunityPopwindow();
        initPickerPopwindow();

        communityHttpModel.communityRequestThings();


    }

    private void initData(){
        txtTitle.setText("出售物件");
        txtHouse.setText("土地");
        requestVO= LocalInfo.getLandProperty(PublishedLandActivity.this,type);
        if("".equals(requestVO.getProperty_purpose())||requestVO.getProperty_purpose()==null){
            requestVO.setProperty_purpose("land");
        }
        requestVO.setProperty_modal(Integer.parseInt(type));
        if("0".equals(type)){
            txtModal.setText("住宅用地");
        }
        else if("1".equals(type)){
            txtModal.setText("商業用地");
        }
        else if("2".equals(type)){
            txtModal.setText("工業用地");
        }
        else if("3".equals(type)){
            txtModal.setText("農地");
        }
        else if("4".equals(type)){
            txtModal.setText("農牧地");
        }
        else if("5".equals(type)){
            txtModal.setText("山林牧地");
        }
        else if("6".equals(type)){
            txtModal.setText("墓地");
        }
        else if("7".equals(type)){
            txtModal.setText("休閑用地");
        }
        else if("8".equals(type)){
            txtModal.setText("停車場用地");
        }
        else if("9".equals(type)){
            txtModal.setText("道路用地");
        }
        else if("10".equals(type)){
            txtModal.setText("畸零地");
        }

        else if("11".equals(type)){
            txtModal.setText("礦鹽地");
        }
        else if("12".equals(type)){
            txtModal.setText("養殖地");
        }
        else if("13".equals(type)){
            txtModal.setText("其他");
        }
    }

    private void fillData(){

        etName.setText(requestVO.getProperty_name());

        //售价
        if(requestVO.getProperty_saleprice()!=0){
            etSellprice.setText(String.valueOf(requestVO.getProperty_saleprice()));
        }

        //坪数
        if(requestVO.getProperty_ownershiparea()!=0){
            etSquare.setText(String.valueOf(requestVO.getProperty_ownershiparea()));
        }

        if(!StringUtil.isBlank(String.valueOf(requestVO.getProperty_desc()))){
            et_descrip.setText(String.valueOf(requestVO.getProperty_desc()));
        }

        //公開
        if(requestVO.getProperty_lift()==1){
            check_lift.setChecked(true);
        }
        else{
            check_lift.setChecked(false);
        }
        //不公開
        if(requestVO.getProperty_carport()==1){
            check_carport.setChecked(true);
        }
        else{
            check_carport.setChecked(false);
        }

        //       Log.e( "requestVO.getPreetype()",""+requestVO.getProperty_feetype());
        //段
        if(!StringUtil.isBlank(requestVO.getProperty_locate1())){
            et_locate1.setText(requestVO.getProperty_locate1());
        }
        if(!StringUtil.isBlank(requestVO.getProperty_locate2())){
            et_locate2.setText(requestVO.getProperty_locate2());
        }
        //屬性
        if(!StringUtil.isBlank(requestVO.getProperty_landapplication())){
            landapplication=requestVO.getProperty_landapplication();
            StringBuffer text = fillattribute(requestVO.getProperty_landapplication());
            et_landapplication.setText(text.toString());

        }
        if(!StringUtil.isBlank(requestVO.getProperty_landattribute())){
            landattribute=requestVO.getProperty_landattribute();
            StringBuffer text = fillEquip(requestVO.getProperty_landattribute());
            et_landattribute.setText(text.toString());

        }
        //图片

        if(requestVO.getPropertyimg_sceimgtype()!=null && requestVO.getPropertyimg_sceimgtype().length!=0){
            //  Log.e("requestVOgtype()",""+requestVO.getPropertyimg_sceimgtype().length);
            String[] imgs = new String[3];
            imgs[0]="";
            fillImg(imgs);
        }
        //地址
        if(requestVO.getProperty_city()!=0){
            InputStream cityinputStream = getResources().openRawResource(R.raw.city);
            List<ZoneVO> cityVOs = Tools.getString("15801",cityinputStream,"",String.valueOf(requestVO.getProperty_city()));
            //  Log.e("requestPrty()==",""+requestVO.getProperty_city()+"++"+cityVOs.size());
            if(requestVO.getProperty_area()!=0){
                InputStream areainputStream = getResources().openRawResource(R.raw.area);
                List<ZoneVO> areaVOs = Tools.getString(cityVOs.get(0).getId(),areainputStream,"",String.valueOf(requestVO.getProperty_area()));
                etAddr.setText(cityVOs.get(0).getName()+areaVOs.get(0).getName());
                Log.e("requestPrty()==",""+requestVO.getProperty_street()+"++"+cityVOs.size());
                if(!StringUtil.isBlank(requestVO.getProperty_street())){
                    InputStream streetinputStream = getResources().openRawResource(R.raw.street);
                    List<ZoneVO> streetVOs = Tools.getString(areaVOs.get(0).getId(),streetinputStream,"",String.valueOf(requestVO.getProperty_street()));
                    if(streetVOs.size()>0){
                        etAddr.setText(cityVOs.get(0).getName()+areaVOs.get(0).getName()+streetVOs.get(0).getName());
                    }

                }
            }

        }

    }

    private void initCheck(){
// if(check_room_age.isChecked()){

        check_lift.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    check_carport.setChecked(false);
                }
            }
        });
        check_carport.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    check_lift.setChecked(false);
                }
            }
        });
    }

    private void fillImg(String[] imgs){

        ll_img_item.setVisibility(View.VISIBLE);
        String[] image = null;

//        ll_img.removeAllViews();
        Log.e("getPropertysceimgtype==",""+requestVO.getPropertyimg_sceimgtype().length);
        for(int i=0;i<requestVO.getPropertyimg_sceimgtype().length;i++){
            if(!StringUtil.isBlank(requestVO.getPropertyimg_sceimgtype()[i])){
//                Log.e("getProperimgtype111==",""+(requestVO.getPropertyimg_sceimgtype()[i]==null));
//                Log.e("getProperimgtype111==",""+(requestVO.getPropertyimg_sceimgtype()[i]));
                FrameLayout content = new FrameLayout(activity);
                FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, 220);
                lp.setMargins(40,40,0,10);
                content.setLayoutParams(lp);

                TextView txt = new TextView(activity);
                FrameLayout.LayoutParams txtlp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                txtlp.setMargins(20,0,0,10);
                txtlp.gravity= Gravity.BOTTOM;
                txt.setLayoutParams(txtlp);

                RollPagerView pagerView = new RollPagerView(activity);

                ChooseImgVO img = new ChooseImgVO();

                if(imgs==null || imgs.length==0){
                    image=mytype.get(requestVO.getPropertyimg_sceimgtype()[i]).getImg();

                    if(chooseImg.get(requestVO.getPropertyimg_sceimgtype()[i])==null){

                        img.setFrameLayout(content);
                        img.setTextView(txt);
                        img.setPagerView(pagerView);
                        chooseImg.put(requestVO.getPropertyimg_sceimgtype()[i],img);

                    }
                    else{
                        content=chooseImg.get(requestVO.getPropertyimg_sceimgtype()[i]).getFrameLayout();
                        txt=chooseImg.get(requestVO.getPropertyimg_sceimgtype()[i]).getTextView();
                        pagerView=chooseImg.get(requestVO.getPropertyimg_sceimgtype()[i]).getPagerView();
                    }

                    requestVO.setPropertyimg_path2(image);
                }
                else{
                    img.setFrameLayout(content);
                    img.setTextView(txt);
                    img.setPagerView(pagerView);
                    chooseImg.put("1",img);
                    image=requestVO.getPropertyimg_path2();
                }

                if("0".equals(requestVO.getPropertyimg_sceimgtype()[i])){
                    txt.setText("社區實景圖");
                }
                else if("1".equals(requestVO.getPropertyimg_sceimgtype()[i])){
                    txt.setText("效果圖");
                }
                else if("2".equals(requestVO.getPropertyimg_sceimgtype()[i])){
                    txt.setText("規劃圖");
                }
                else if("3".equals(requestVO.getPropertyimg_sceimgtype()[i])){
                    txt.setText("位置圖");
                }
                else if("4".equals(requestVO.getPropertyimg_sceimgtype()[i])){
                    txt.setText("生活機能圖");
                }
                else if("5".equals(requestVO.getPropertyimg_sceimgtype()[i])){
                    txt.setText("樣品屋圖");
                }
                else if("6".equals(requestVO.getPropertyimg_sceimgtype()[i])){
                    txt.setText("室內實景圖");
                }

                txt.setTextColor(getResources().getColor(R.color.white));
                if(image!=null && image.length!=0){
                    pagerView.setAdapter(new ImageLoopAdapter(pagerView,image,requestVO.getPropertyimg_sceimgtype()[i]));
                }


                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    pagerView.setHintView(new ColorPointHintView(activity, Color.parseColor("#DC473A"),Color.parseColor("#88ffffff")));
                }
                pagerView.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(int position) {
                        //   Toast.makeText(PublishedActivity.this,"图片 "+position+" clicked",Toast.LENGTH_SHORT).show();
                    }
                });

                if(content.getChildCount()!=0){
                    content.removeAllViews();

                    content.addView(pagerView);
                    content.addView(txt);
                }
                else{
                    content.addView(pagerView);
                    content.addView(txt);

                    ll_img.addView(content);
                }
            }

        }

        if(imgs==null || imgs.length==0){
            LocalInfo.saveProperty(activity,requestVO);
        }

    }

    private void initRequest(){

        propertyModel = new ThingsHttpModel(RequestUrl.savepropertyUrl, params ,
                new ThingsHttpModel.SimpleOnHttpRequestListener() {

                    @Override
                    public void onRequestFinished(String resultCode, Integer timestamp) {

                    }

                    @Override
                    public void onRequestFinished(String resultCode, Object property, Object user, Object users, Object circuit, Object img) {

                    }

                    @Override
                    public void onBuildingRequestFinished(String resultCode, Object building, Object user, Object users, Object circuit, Object img) {

                    }

                    @Override
                    public void onCommunityRequestFinished(String resultCode, Object community, Object user, Object users, Object circuit, Object img) {

                    }
                }
                ,true);

        communityHttpModel= new ThingsHttpModel(RequestUrl.communityUrl, params ,
                new ThingsHttpModel.SimpleOnHttpRequestListener() {

                    @Override
                    public void onRequestFinished(String resultCode, Integer timestamp) {

                    }

                    @Override
                    public void onRequestFinished(String resultCode, Object property, Object user, Object users, Object circuit, Object img) {

                    }

                    @Override
                    public void onBuildingRequestFinished(String resultCode, Object building, Object user, Object users, Object circuit, Object img) {

                    }

                    @Override
                    public void onCommunityRequestFinished(String resultCode, Object community, Object user, Object users, Object circuit, Object img) {
                        communityList.clear();
                        Gson gson = new Gson();
                        List<RentVO>  model = gson.fromJson(gson.toJson(community), new TypeToken<List<RentVO>>() {}.getType());
//                        for(RentVO vo :model){
                        communityList.addAll(model);
//                        }


                    }
                }
                ,true);

    }
    private void initWatcher(){

        TextWatcher nwname=new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {


            }

            @Override
            public void afterTextChanged(Editable s) {
                if(etName.getText().length()>10){
                    MyAlertDialog dialog = new MyAlertDialog(PublishedLandActivity.this,"物件名稱不能超過10個漢字",String.valueOf(etName.getText()));
                    dialog.show(PublishedLandActivity.this,"物件名稱不能超過10個漢字",1);
                }
                else{
                    if(!StringUtil.isBlank(String.valueOf(etName.getText()))){
                        requestVO.setProperty_name(String.valueOf(etName.getText()));
                        LocalInfo.saveProperty(PublishedLandActivity.this,requestVO);
                    }
                }


            }
        };
        etName.addTextChangedListener(nwname);
        etName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                //得到焦點
                if (hasFocus) {
                    llPropertyBottom.setVisibility(View.VISIBLE);
                    picker.setVisibility(View.GONE);

                }

            }
        });

        TextWatcher cname=new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                if (communitypop != null && communitypop.isShowing()) {
//                    communitypop.dismiss();
//                } else {
                ll_community.setBackgroundResource(R.drawable.community_shape);
                if(!StringUtil.isBlank(String.valueOf(etCommunity.getText()))){

                    params.put("searchKey",String.valueOf(etCommunity.getText()));
                }
                communityHttpModel.communityRequestThings();
                communityAdapter.notifyDataSetChanged();
                communitypop.showAsDropDown(etCommunity);
//                }

            }

            @Override
            public void afterTextChanged(Editable s) {

                ll_community.setBackgroundResource(R.drawable.edit_community_shape);


            }
        };
        etCommunity.addTextChangedListener(cname);


    }

    @Override
    public void setListener() {

        //售价
        etSellprice.addTextChangedListener(new CustomTextWatcher(activity,etSellprice,requestVO,6));
        //坪数
        etSquare.addTextChangedListener(new CustomTextWatcher(activity,etSquare,requestVO,9));
        et_descrip.addTextChangedListener(new CustomTextWatcher(activity,et_descrip,requestVO,15));

        et_locate1.addTextChangedListener(new CustomTextWatcher(activity,et_locate1,requestVO,19));
        et_locate2.addTextChangedListener(new CustomTextWatcher(activity,et_locate2,requestVO,20));
        etAddr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PublishedChooseAddrActivity.gotoActivity(activity,"1");

            }
        });
        et_landapplication.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PublishedChooseShopEquipActivity.gotoActivity(activity,"2",landapplication);
            }
        });
        et_landattribute.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PublishedChooseShopEquipActivity.gotoActivity(activity,"3",landattribute);
            }
        });
        relImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                PublishedChooseImageActivity.gotoActivity(activity);

            }
        });

        myfocusDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View rootview = PublishedLandActivity.this.getWindow().getDecorView();
                View focus = rootview.findFocus();
                if(findViewById(focus.getNextFocusDownId())!=null && focus.getNextFocusDownId()!=0){
                    findViewById(focus.getNextFocusDownId()).requestFocus();
                }


            }
        });
        myfocusUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View rootview = PublishedLandActivity.this.getWindow().getDecorView();
                View focus = rootview.findFocus();
                if(findViewById(focus.getNextFocusDownId())!=null && focus.getNextFocusUpId()!=0){
                    findViewById(focus.getNextFocusUpId()).requestFocus();
                }


            }
        });

        llBottomSee.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Boolean notice=false;
                //售价
                if(requestVO.getProperty_saleprice()==0){
                    setFocus(etSellprice,notice);
                }

                //坪数
                if(requestVO.getProperty_ownershiparea()==0){
                    setFocus(etSquare,notice);
                }

                if(StringUtil.isBlank(String.valueOf(requestVO.getProperty_desc()))){
                  //  setFocus(et_descrip,notice);
                }

                //       Log.e( "requestVO.getPreetype()",""+requestVO.getProperty_feetype());
                //段
                if(StringUtil.isBlank(requestVO.getProperty_locate1())){
                    setFocus(et_locate1,notice);
                }
                if(StringUtil.isBlank(requestVO.getProperty_locate2())){
                    setFocus(et_locate2,notice);
                }
                //屬性
                if(StringUtil.isBlank(requestVO.getProperty_landapplication())){
                    setFocus(et_landapplication,notice);

                }
                if(StringUtil.isBlank(requestVO.getProperty_landattribute())){

                    setFocus(et_landattribute,notice);
                }
                if(notice){

                    MyAlertDialog dialog = new MyAlertDialog(activity,"您尚未填寫完畢",String.valueOf(etName.getText()));
                    dialog.show(activity,"您尚未填寫完畢",1);
                }
                else{

                    ThingsLandDetailActivity.gotoActivity(activity,"2","6",type,"");
                }

            }
        });

    }

    private static void setFocus(EditText edit,boolean notice){

        notice=true;
        edit.requestFocus();
        InputMethodManager imm=(InputMethodManager)edit.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);

        imm.toggleSoftInput(0,InputMethodManager.SHOW_FORCED);


    }


    private void initPopwindow(){
        LayoutInflater myinflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View contentView=myinflater.inflate(R.layout.popwindow_picker, null);


        pop = new PopupWindow(contentView,
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT, true);

        pop.setTouchable(true);
        //  ColorDrawable dw = new ColorDrawable(getResources().getColor(R.color.backgroud_light_I));
        pop.setBackgroundDrawable(new BitmapDrawable());


    }

    private void initPickerPopwindow(){

        LayoutInflater myinflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View contentView=myinflater.inflate(R.layout.popwindow_picker, null);


        pickerpop = new PopupWindow(contentView,
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, true);

        pickerpop.setTouchable(true);

        focusDown = (ImageView)contentView.findViewById(R.id.focus_down);
        focusUp = (ImageView)contentView.findViewById(R.id.focus_up);

        picker= (PickerView)contentView.findViewById(R.id.choose_picker);
        choose_month_picker= (PickerView)contentView.findViewById(R.id.choose_month_picker);
        choose_year_unit= (TextView)contentView.findViewById(R.id.choose_year_unit);
        choose_month_unit= (TextView)contentView.findViewById(R.id.choose_month_unit);
        //  ColorDrawable dw = new ColorDrawable(getResources().getColor(R.color.backgroud_light_I));
        pickerpop.setBackgroundDrawable(new BitmapDrawable());

    }


    private void initCommunityPopwindow(){
        LayoutInflater myinflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View contentView=myinflater.inflate(R.layout.popwindow_community, null);


        communitypop = new PopupWindow(contentView,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, true);
        ListView community_list = (ListView)contentView.findViewById(R.id.community_list);

        communityAdapter = new CommunityAdapter(PublishedLandActivity.this);
        community_list.setAdapter(communityAdapter);

        communitypop.setTouchable(true);
        //  ColorDrawable dw = new ColorDrawable(getResources().getColor(R.color.backgroud_light_I));
        communitypop.setBackgroundDrawable(new BitmapDrawable());


    }


    @Override
    public void doEvent() {

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==11&&data!=null){
            String city = data.getStringExtra("property_city");
            String cityId = data.getStringExtra("property_city_id");
            String area = data.getStringExtra("property_area");
            String areaId = data.getStringExtra("property_area_id");
            String street = data.getStringExtra("property_street");
            String streetId = data.getStringExtra("property_street_id");
            etAddr.setText(city+area+street);
            if(requestVO==null){
                requestVO=new PropertyCommitVO();
            }
            Log.e("streetIdstreetI===",""+cityId);
            if(!StringUtil.isBlank(cityId)){
                requestVO.setProperty_city(Integer.parseInt(cityId));
            }
            if(!StringUtil.isBlank(areaId)){
                requestVO.setProperty_area(Integer.parseInt(areaId));
            }
            if(!StringUtil.isBlank(streetId)){
                requestVO.setProperty_street(streetId);
            }

            LocalInfo.saveProperty(activity,requestVO);
            //  PublishedActivity.this.setResult(RESULT_OK);
        }
        //类型
        else if(requestCode==14&&data!=null){
            String property_equipment = data.getStringExtra("property_equipment");
            StringBuffer text = fillEquip(property_equipment);

            et_landapplication.setText(text.toString());
            requestVO.setProperty_landapplication(property_equipment);
            LocalInfo.saveProperty(activity,requestVO);

        }
        //属性
        else if(requestCode==15&&data!=null){
            String property_equipment = data.getStringExtra("property_equipment");
            StringBuffer text = fillattribute(property_equipment);

            et_landattribute.setText(text.toString());
            requestVO.setProperty_landattribute(property_equipment);
            LocalInfo.saveProperty(activity,requestVO);

        }
        //選擇圖片
        else if(requestCode==1&&data!=null){
            String selectedImages = data.getStringExtra("selectedImages");
            String height = data.getStringExtra("height");
            String width = data.getStringExtra("width");
            String size = data.getStringExtra("size");
            String name = data.getStringExtra("name");
            Log.e("widthwidth==",""+height+selectedImages);
            requestVO.setPropertyimg_width(width.split(","));
            requestVO.setPropertyimg_height(height.split(","));
            requestVO.setPropertyimg_size(size.split(","));
            requestVO.setPropertyimg_name(name.split(","));
            String imgtype = data.getStringExtra("type");

            ThingImageVO imageVO = new ThingImageVO();
            imageVO.setType(imgtype);

            if(mytype.get(imgtype)==null){
                mytype.put(imgtype,imageVO);
            }

            String[] type1 = new String[7];
            Iterator<Map.Entry<String, ThingImageVO>> it = mytype.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<String, ThingImageVO> entry = it.next();
                type1[Integer.parseInt(entry.getKey())]=entry.getValue().getType();
            }

            requestVO.setPropertyimg_sceimgtype(type1);

            String[] img = selectedImages.split(",");
            requestVO.setPropertyimg_path1(img);

            if(img.length<3){
                String[] myimg = new String[img.length+1];
                if(img.length==1){
                    myimg[0]=img[0];
                    myimg[1]="";
                }
                else if(img.length==2){
                    myimg[0]=img[0];
                    myimg[1]=img[1];
                    myimg[2]="";
                }
                mytype.get(imgtype).setImg(myimg);
            }
            else if(img.length==3){
                mytype.get(imgtype).setImg(img);
            }

            fillImg(null);
            LocalInfo.saveProperty(activity,requestVO);

        }
    }

    private StringBuffer fillattribute(String property_equipment){

        String[] equipment = property_equipment.split(",");
        StringBuffer text = new StringBuffer("");
        for (int i = 0; i < equipment.length; i++) {
            if (!"".equals(equipment[i])) {

                if ("1".equals(equipment[i])) {
                    text.append("住宅區");
                    if (i != equipment.length - 1) {
                        text.append(",");
                    }

                } else if ("2".equals(equipment[i])) {
                    text.append("商業區");
                    if (i != equipment.length - 1) {
                        text.append(",");
                    }
                }
                else if ("3".equals(equipment[i])) {
                    text.append("工業用地");
                    if (i != equipment.length - 1) {
                        text.append(",");
                    }
                }
                else if ("4".equals(equipment[i])) {
                    text.append("文教區");
                    if (i != equipment.length - 1) {
                        text.append(",");
                    }
                }
                else if ("5".equals(equipment[i])) {
                    text.append("風景區");
                    if (i != equipment.length - 1) {
                        text.append(",");
                    }
                }
                else if ("6".equals(equipment[i])) {
                    text.append("保護區");
                    if (i != equipment.length - 1) {
                        text.append(",");
                    }
                }
                else if ("7".equals(equipment[i])) {
                    text.append("行政區");
                    if (i != equipment.length - 1) {
                        text.append(",");
                    }
                }
                else if ("8".equals(equipment[i])) {
                    text.append("特定農業區");
                    if (i != equipment.length - 1) {
                        text.append(",");
                    }
                }
                else if ("9".equals(equipment[i])) {
                    text.append("一般農業區");
                    if (i != equipment.length - 1) {
                        text.append(",");
                    }
                }
                else if ("10".equals(equipment[i])) {
                    text.append("鄉村區");
                    if (i != equipment.length - 1) {
                        text.append(",");
                    }
                }
                else if ("11".equals(equipment[i])) {
                    text.append("森林區");
                    if (i != equipment.length - 1) {
                        text.append(",");
                    }
                }
                else if ("12".equals(equipment[i])) {
                    text.append("山坡地保育區");
                    if (i != equipment.length - 1) {
                        text.append(",");
                    }
                }
                else if ("13".equals(equipment[i])) {
                    text.append("公園區");
                    if (i != equipment.length - 1) {
                        text.append(",");
                    }
                }
                else if ("14".equals(equipment[i])) {
                    text.append("河川區");
                    if (i != equipment.length - 1) {
                        text.append(",");
                    }
                }
                else if ("15".equals(equipment[i])) {
                    text.append("其他或專用區");
                    if (i != equipment.length - 1) {
                        text.append(",");
                    }
                }

            }

        }

        return text;
    }
    private StringBuffer fillEquip(String property_equipment) {
        String[] equipment = property_equipment.split(",");
        StringBuffer text = new StringBuffer("");
        for (int i = 0; i < equipment.length; i++) {
            if (!"".equals(equipment[i])) {
                if ("1".equals(equipment[i])) {
                    text.append("甲種建築用地");
                    if (i != equipment.length - 1) {
                        text.append(",");
                    }

                } else if ("2".equals(equipment[i])) {
                    text.append("乙種建築用地");
                    if (i != equipment.length - 1) {
                        text.append(",");
                    }
                } else if ("3".equals(equipment[i])) {
                    text.append("丙種建築用地");
                    if (i != equipment.length - 1) {
                        text.append(",");
                    }
                } else if ("4".equals(equipment[i])) {
                    text.append("丁種建築用地");
                    if (i != equipment.length - 1) {
                        text.append(",");
                    }
                } else if ("5".equals(equipment[i])) {
                    text.append("農牧用地");
                    if (i != equipment.length - 1) {
                        text.append(",");
                    }
                } else if ("6".equals(equipment[i])) {
                    text.append("養殖用地");
                    if (i != equipment.length - 1) {
                        text.append(",");
                    }
                } else if ("7".equals(equipment[i])) {
                    text.append("遊憩用地");
                    if (i != equipment.length - 1) {
                        text.append(",");
                    }
                } else if ("8".equals(equipment[i])) {
                    text.append("墳墓用地");
                    if (i != equipment.length - 1) {
                        text.append(",");
                    }
                } else if ("9".equals(equipment[i])) {
                    text.append("鹽業用地");
                    if (i != equipment.length - 1) {
                        text.append(",");
                    }
                }
            } else if ("10".equals(equipment[i])) {
                text.append("鹽業用地");
                if (i != equipment.length - 1) {
                    text.append(",");
                }

                } else if ("11".equals(equipment[i])) {
                text.append("林業用地");
                if (i != equipment.length - 1) {
                    text.append(",");
                }

                } else if ("12".equals(equipment[i])) {
                text.append("礦業用地");
                if (i != equipment.length - 1) {
                    text.append(",");
                }

                } else if ("13".equals(equipment[i])) {
                text.append("窯業用地");
                if (i != equipment.length - 1) {
                text.append(",");
                }

                } else if ("14".equals(equipment[i])) {
                text.append("交通用地");
                if (i != equipment.length - 1) {
                    text.append(",");
                }
            }
                else if ("15".equals(equipment[i])) {
                    text.append("水利用地");
                    if (i != equipment.length - 1) {
                        text.append(",");
                    }
                }
                    else if ("16".equals(equipment[i])) {
                    text.append("古跡保存用地");
                    if (i != equipment.length - 1) {
                        text.append(",");
                    }
                }
                        else if ("17".equals(equipment[i])) {
                    text.append("生態保護用地");
                    if (i != equipment.length - 1) {
                        text.append(",");
                    }
                }
                            else if ("18".equals(equipment[i])) {
                    text.append("國土保安用地");
                    if (i != equipment.length - 1) {
                        text.append(",");
                    }
                }
                                else if ("19".equals(equipment[i])) {
                    text.append("特定目的事業用地");
                    if (i != equipment.length - 1) {
                        text.append(",");
                    }
                }
                else if ("20".equals(equipment[i])) {
                        text.append("其他");
                            if (i != equipment.length - 1) {
                                  text.append(",");
                            }
                }

             }
                 return text;
          }

    private class CommunityAdapter extends BaseAdapter {

        private LayoutInflater mInflater;
        public CommunityAdapter(Context context){
            this.mInflater = LayoutInflater.from(context);
        }
        @Override
        public int getCount() {
            return communityList.size();
        }

        @Override
        public Object getItem(int i) {
            return communityList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View  convertView = mInflater.inflate(R.layout.txt_item, null);
            final RentVO obj = communityList.get(i);
            TextView txt_content = (TextView)convertView.findViewById(R.id.txt_content);
            txt_content.setText(obj.getName());
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    etCommunity.setText(obj.getName());
                    requestVO.setProperty_community(obj.getSeq());
                    LocalInfo.saveProperty(PublishedLandActivity.this,requestVO);
                    if (communitypop != null && communitypop.isShowing()) {
                        communitypop.dismiss();
                    }
                }
            });
            return convertView;
        }
    }

    private class ImageLoopAdapter extends LoopPagerAdapter {

        private String[] imgs=null;
        private String mytype;
        public ImageLoopAdapter(RollPagerView viewPager,String[] imgs,String type) {

            super(viewPager);
            this.imgs=imgs;
            mytype=type;
        }

        @Override
        public View getView(ViewGroup container, final int position) {
            SimpleDraweeView view = new SimpleDraweeView(container.getContext());
            view.setScaleType(ImageView.ScaleType.CENTER_CROP);
            if(StringUtil.isBlank(imgs[position])){
                view.setLayoutParams(new ViewGroup.LayoutParams(20, 20));
                view.setImageResource(R.mipmap.btn_est_upload_nor2x);
            }
            else{
//                view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//                view.setImageURI(Uri.parse("file://"+imgs[position].toString()));
                ImageLoader.loadFile(view,imgs[position].toString(),200,200);
            }

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PublishedImageActivity.gotoActivity(activity,mytype,"6",type,"");
                }
            });
            return view;

        }

        @Override
        public int getRealCount() {
            return imgs.length;
        }


    }

}